#!/bin/bash
# date: 2025/4/27
# desc: install apollo-service-1.8.0 apollo-portal-1.8.0

install() {
    # mysql -h127.0.0.1 -P3306 -root -p < 180-apolloconfig.sql
    # mysql -h127.0.0.1 -P3306 -root -p < 180-apolloport.sql

    kubectl create ns apollo
    helm install -n apollo apollo-service -f apollo-service/values.yaml apollo-service

    # http://127.0.0.1:8070
    helm install -n apollo apollo-portal -f apollo-portal/values.yaml apollo-portal
    kubectl apply -n apollo -f apollo-portal/apisixroute.yaml
}

uninstall() {
    helm uninstall -n apollo apollo-service
    helm uninstall -n apollo apollo-portal
    kubectl delete ns apollo
}

apollo_ls() {
    helm -n apollo ls
    kubectl -n apollo get pods
    kubectl -n apollo get svc
}

apollo_config_api() {
    appid=test
    cluster=default
    namespace=application
    env=TEST
    # curl -s -X GET "http://127.0.0.1:8080/configs/test/default/application?env=TEST"
    curl -s -X GET "http://127.0.0.1:8080/configs/${appid}/${cluster}/${namespace}?env=${env}" | jq
}

# apollo管理页面 - 管理员工具 - 开放平台授权管理 - 创建全局token
apollo_portal_api() {
    appid=test
    cluster=default
    namespace=application
    env=TEST
    curl -s -X GET "http://127.0.0.1:8070/openapi/v1/envs/${env}/apps/${appid}/clusters/${cluster}/namespaces" --header 'authorization: token' --header 'content-type: application/json;charset=UTF-8'
}

case "$1" in
install)
    install
    ;;
uninstall)
    uninstall
    ;;
ls)
    apollo_ls
    ;;
conf)
    apollo_config_api
    # apollo_portal_api
    ;;
*)
    echo "$0 install|uninstall|ls|conf"
    ;;
esac
